package com.kc.business.service.impl;

import com.kc.business.domain.BizScenic;
import com.kc.business.domain.BizScenicCultural;
import com.kc.business.mapper.BizScenicCulturalMapper;
import com.kc.business.mapper.BizScenicMapper;
import com.kc.business.service.IBizScenicCulturalService;
import com.kc.common.exception.CustomException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * TODO
 *
 * @author zhangshuo
 * @version 1.0
 * @date 2020/9/24
 */
@Service
public class BizScenicCulturalServiceImpl implements IBizScenicCulturalService {
    @Resource
    BizScenicCulturalMapper scenicCulturalMapper;
    @Resource
    BizScenicMapper scenicMapper;

    /**
     * 文创征集列表
     *
     * @param scenicCultural
     * @return
     */
    @Override
    public List<BizScenicCultural> getList(BizScenicCultural scenicCultural) {
        return scenicCulturalMapper.select(scenicCultural);
    }

    /**
     * 新增文创征集
     *
     * @param scenicCultural
     * @return
     */
    @Override
    @Transactional
    public int add(BizScenicCultural scenicCultural) {
        BizScenic scenic = scenicMapper.selectByPrimaryKey(scenicCultural.getScenicId());
        if (!ObjectUtils.isEmpty(scenic)) {
            scenicCultural.setScenicName(scenic.getScenicName());
            scenicCultural.setLongitude(scenic.getLongitude());
            scenicCultural.setLatitude(scenic.getLatitude());
        } else {
            throw new CustomException("景区不存在");
        }
        return scenicCulturalMapper.insertSelective(scenicCultural);
    }

    /**
     * 修改文创征集
     *
     * @param scenicCultural
     * @return
     */
    @Override
    public int edit(BizScenicCultural scenicCultural) {
        BizScenic scenic = scenicMapper.selectByPrimaryKey(scenicCultural.getScenicId());
        if (!ObjectUtils.isEmpty(scenic)) {
            scenicCultural.setScenicName(scenic.getScenicName());
            scenicCultural.setLongitude(scenic.getLongitude());
            scenicCultural.setLatitude(scenic.getLatitude());
        } else {
            throw new CustomException("景区不存在");
        }
        return scenicCulturalMapper.updateByPrimaryKeySelective(scenicCultural);
    }
}
